package com.liang.rlock.impl;

import com.liang.config.RedissonConnector;
import com.liang.rlock.AquiredLockWorker;
import com.liang.rlock.DistributedLocker;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created with Intellij IDEA
 *
 * @Auther: liangjy
 * @Date: 2022/01/06/17:03
 * @Description:实现分布式锁管理接口
 */
@Component
public class RedisLock implements DistributedLocker {
    private final static String name = "redisLock";
    @Autowired
    private RedissonConnector redissonConnector;

    @Override
    public <T> T lock(String resourceName, AquiredLockWorker<T> worker) throws Exception {
        return lock(resourceName, worker, 100);
    }

    @Override
    public <T> T lock(String resourceName, AquiredLockWorker<T> worker, long time) throws Exception {
        RedissonClient redissonClient = redissonConnector.getRedissonClient();
        RLock lock = redissonClient.getLock(name + resourceName);
        //等待100秒释放锁 最多阻塞100s 过多少time 锁释放
        boolean flag = lock.tryLock(100, time, TimeUnit.SECONDS);
        if (flag) {
            //代码必须这样设计
            try {
                //拿取到锁之后执行的业务的方法
                return worker.invokeAfterLockAquire();
            } finally {
                lock.unlock();
            }
        }

        //没有拿取到锁时，会报没有拿取锁异常
        throw new UnsupportedOperationException();
    }
}
